import Cookies from "js-cookie";

export const setCookie = (key: string, value: any, expiresTime?: number) => {
	if (expiresTime) {
		const seconds = expiresTime;
		const expires = new Date(+new Date() * 1 + seconds * 1000);
		return Cookies.set(key, value, { expires: expires });
	}
	return Cookies.set(key, value);
};

export const getToken = () => {
	return Cookies.get("ACCESS__TOKEN__") || "";
};

export const getCookie = (key: string) => {
	return Cookies.get(key) || "";
};

export const removeCookie = (key: string) => {
	Cookies.remove(key);
};

export function removeExpiresTime() {
	Cookies.remove("expires");
}

export function setToken(token: any, expires: any) {
	Cookies.set("expires", expires);
	// linux 时间，需要拼 000
	return Cookies.set("ACCESS__TOKEN__", token, {
		expires: expires ? new Date(parseInt(expires + "000")) : 1
	});
}

export const removeToken = () => {
	Cookies.remove("ACCESS__TOKEN__");
	Cookies.remove("expires");
	Cookies.remove("USER_INFO__");
};
